<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;
use app\common\model\Config;

class Play extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'play';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';
    protected $dateFormat = 'Y-m-d H:i:s';

    // 定义时间戳字段名
    protected $createTime = 'create_dt';
    protected $updateTime = 'update_dt';
    protected $deleteTime = 'delete_dt';

    // 追加属性
    protected $append = [
        'play_status_text',
        'pay_status_text',
        'is_pay_text',
        'scheme_no_text',
        'play_type_text',
        'play_label_text',
        'back_money_text',
        'back_money_text2',
        'img_url',
        'stock_text',
        'start_date_text',
        'end_date_text',
        'play_type'
    ];
    
    
    public function getPayStatusList()
    {
        return ['0' => '待支付', '1' => '部分支付', '2' => '已支付', '-1' => '已退回'];
    }

    public function getIsPayList()
    {
        return ['0' => '后台创建活动', '1' => '余额支付活动'];
    }

    public function getPlayStatusList()
    {
        return ['0' => '待支付', '1' => '待审核', '2' => '进行中', '3' => '待支付', '4' => '已结束', '5' => '已完成', '6' => '驳回'];
    }

    public function getSchemeNoList()
    {
        // 店铺类型
        return Config::getConfigs('play_scheme');
    }

    public function getPlayTypeList()
    {
        // 店铺类型
        return Config::getConfigs('play_type');
    }

    public function getPlayLabelList()
    {
        // 店铺类型
        $play_label = Config::getConfigs('play_label');
        // $play_label[2] = $play_label[3] = '用餐反馈';
        return $play_label;
    }


    public function getPayStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_status']) ? $data['pay_status'] : '');
        $list = $this->getPayStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsPayTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_pay']) ? $data['is_pay'] : '');
        $list = $this->getIsPayList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPlayStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['play_status']) ? $data['play_status'] : '');
        $list = $this->getPlayStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getSchemeNoTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['scheme_no']) ? $data['scheme_no'] : '');
        $list = $this->getSchemeNoList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public static function getPlayTypeAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['play_type']) ? $data['play_type'] : '');
        return $value==3&&$data['is_qcsc']==1 ? 2 : $data['play_type'];
    }


    public function getPlayTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['play_type']) ? $data['play_type'] : '');
        $list = $this->getPlayTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPlayLabelTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['play_label']) ? $data['play_label'] : '');
        $list = $this->getPlayLabelList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getBackMoneyTextAttr($value, $data)
    {
        if ($data['user_pay']==0) {
            $text = '每单返'.(float)$data['back_money'];
        } else {
            $text = '满'.floatval($data['user_pay']).'返'.floatval($data['back_money']);
        }
        return $text;
    }


    public function getBackMoneyText2Attr($value, $data)
    {
        if ($data['user_pay']==0) {
            $text = '每单返'.(float)$data['shop_settlement'];
        } else {
            $text = '满'.floatval($data['user_pay']).'返'.floatval($data['shop_settlement']);
        }
        return $text;
    }


    public function getStockTextAttr($value, $data)
    {
        $text = $data['stock'].'/'.$data['num'];
        return $text;
    }


    public function getStartDateTextAttr($value, $data)
    {
        $text = date('Y-m-d',$data['start_date']);
        return $text;
    }


    public function getEndDateTextAttr($value, $data)
    {
        $text = date('Y-m-d',$data['end_date']);
        return $text;
    }

    public function getImgUrlAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['logo_pic']) ? $data['logo_pic'] : '');
        $value = $value ? $value : (isset($data['img']) ? $data['img'] : '');
        return config('upload_domain').$value;
    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function shop()
    {
        return $this->belongsTo('Shop', 'shop_no', 'shop_no', [], 'LEFT')->setEagerlyType(0);
    }
}
